package com.techysky.shiro;
import org.apache.shiro.authz.AuthorizationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;



import javax.servlet.http.HttpServletRequest;


/**
 * 异常处理器
 */
@RestControllerAdvice
public class BDExceptionHandler {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @ExceptionHandler(AuthorizationException.class)
    @ResponseBody
    public Object handleAuthorizationException(AuthorizationException e, HttpServletRequest request) {
        logger.error(e.getMessage(), e);
        if (HttpServletUtils.jsAjax(request)){
            return R.error(403, "未授权");
        }
        return new ModelAndView("error/403");
  }

    @ExceptionHandler(DataIntegrityViolationException.class)
    public R handleDuplicateKeyException(DataIntegrityViolationException e) {
        logger.error(e.getMessage(), e);
        System.out.println("asdasdasasadsasd");
        return R.error("数据库中已存在该记录");
   }
    
    @ExceptionHandler(DuplicateKeyException.class)
    public R handleDuplicateKeyException(DuplicateKeyException e) {
        logger.error(e.getMessage(), e);
        return R.error("数据库中已存在该记录");
   }
}
